package com.example.server.web.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.server.web.domain.Class;
import com.example.server.web.domain.RespBean;
import com.example.server.web.domain.Student;
import com.example.server.web.domain.dto.ClassStudentDto;
import com.example.server.web.domain.page.PageQueryDto;
import com.example.server.web.domain.dto.StudentDto;
import com.example.server.web.exception.CustomException;
import com.example.server.web.service.ClassService;
import com.example.server.web.service.StudentService;
import com.example.server.web.service.SysUserService;
import com.example.server.web.service.UserRoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 作者
 * @since 2023-04-04
 */
@RestController
@RequestMapping("/class")
@Api(tags = {"班级接口"})
public class ClassController {
    @Autowired
    ClassService classService;

    @Autowired
    SysUserService sysUserService;

    @Autowired
    StudentService studentService;

    @Autowired
    UserRoleService userRoleService;

    @PostMapping("/queryByPage")
    @ApiOperation("分页查询班级")
    public RespBean queryByPage(@Validated @RequestBody PageQueryDto dto){
        Map map = classService.queryByPage(dto);
        return  RespBean.success("查询成功",map);
    }

    @PostMapping("/showClassStudent")
    @ApiOperation("查询指定班级学生列表")
    public RespBean showClassStudent(@Validated @RequestBody PageQueryDto dto){
        Page<StudentDto> page = studentService.getStuPage(dto);
        List<StudentDto> studentList = page.getRecords();
        Long total = page.getTotal();
        HashMap<String,Object> ans = new HashMap<>();
        ans.put("studentList",studentList);
        ans.put("total",total);
        return  RespBean.success("查询成功",ans);
    }

    @PutMapping("/add")
    @ApiOperation("添加班级信息")
    public RespBean add(@Validated @RequestBody Class c){
        Integer i = classService.add(c);
        if (i == 0) {
            return  RespBean.error(401,"添加失败",null);
        }
        return  RespBean.success("添加成功");
    }

    @PutMapping("/addStu")
    @ApiOperation("为班级添加学生")
    public RespBean add(@Validated @RequestBody ClassStudentDto students){
        List<Student> studentList = new ArrayList<>();//存储添加学生名单
        List<String> usernames = students.getUsernames();
        for(String username:usernames){
            if(userRoleService.judgeRoleByUsername(username,3L)>0){
                // if(studentService.ifExistStudent(username)){
                //     return  RespBean.error(401,"添加列表中存在已经有班级的学生",null);
                // }
                //根据学号查询学生
                LambdaQueryWrapper<Student> queryWrapper = new LambdaQueryWrapper<>();
                queryWrapper.eq(Student::getStuId,username);
                Student student = studentService.getOne(queryWrapper);

                student.setClassId(students.getClassId());//设置加入班级
                student.setStuId(username);
                student.setUserId(sysUserService.getUserByUserName(username).getId());
                studentList.add(student);
            }else{
                return  RespBean.error(401,"添加列表中存在非学生用户",null);
            }
        }
        Integer i = studentService.addStudent(studentList);
        if (i == 0) {
            return  RespBean.error(401,"添加失败",null);
        }
        classService.updateStudentCount(students.getClassId(),i);
        return  RespBean.success("添加成功");
    }

    @PostMapping("/modify")
    @ApiOperation("修改班级信息")
    public  RespBean modify(@Validated @RequestBody Class c){
        Integer i = classService.modify(c);
        if (i == 0) {
            return  RespBean.error(401,"更新失败",null);
        }
        return  RespBean.success("更新成功");
    }

    @DeleteMapping("deletes")
    @ApiOperation("（批量或者单个）删除班级信息")
    public  RespBean deletes(@RequestBody List<Integer> ids){
        Integer i = classService.deletes(ids);
        if (i == 0) {
            return  RespBean.error(401,"删除失败",null);
        }
        return  RespBean.success("删除成功");
    }

    @DeleteMapping("/removeStu")
    @ApiOperation("从班级中移除学生")
    public RespBean removeStudent(@RequestBody List<Long> studentList){
        Long classId = studentService.getStuClass(studentList.get(0));
        Integer i = studentService.removeStudent(studentList);
        if (i == 0) {
            return  RespBean.error(401,"移除失败",null);
        }
        classService.updateStudentCount(classId,i);
        return  RespBean.success("移除成功");
    }

    @ApiOperation("根据学院id查询班级列表")
    @GetMapping("/queryByAcademyId/{academyId}")
    public RespBean queryByAcademyId(@PathVariable("academyId") Long academyId){
        List<Class> classList = classService.queryByAcademyId(academyId);
        return  RespBean.success("查询成功",classList);
    }



}
